package bTree.LeetCode;

import org.junit.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class 识别数组中的最大异常值 {
    public static int getLargestOutlier(int[] nums) {
        int sum = Arrays.stream(nums).sum();
        Map<Integer,Integer>map=new HashMap<>();//记录数值和其对应的次数
        for (int num : nums) {
            if (!map.containsKey(num)){
                map.put(num, 1);
            }else {
                map.put(num, map.get(num)+1);
            }
        }
        int res=Integer.MIN_VALUE;
        for (int num : nums) {
            int i = sum - num;
            if (i%2==1||i%2==-1){
                continue;//防止向下取整，要特判
            }else{
                int i1 = i / 2;
                if (map.containsKey(i1)&&(num!=i1||map.get(i1)>1)){
                    res=Math.max(res, num);
                }
            }
        }
        return res;
    }
}